package model;

import client.ClientView;
import view.DeleteUserView;
import view.EditUserView;
import view.RegisterUserView;
import java.sql.*;

public class User {

    //get instance of connected database
    private boolean connStatus = DBConnection.getInstance().getConnectionActive();
    private ResultSet result = null;
    private boolean userUpdate = false;

    public User() {
        //empty so other classes can access functions
    }

    public static void loadDeleteUserView(String userType) {
        ClientView view = ClientView.getInstance();
        DeleteUserView du = new DeleteUserView(view, userType);
        du.pack();
        view.centerWindow(du);
        du.setVisible(true);
    }

    public static void loadRegisterUserView(String userType) {
        ClientView view = ClientView.getInstance();
        RegisterUserView ru = new RegisterUserView(view, userType);
        ru.pack();
        view.centerWindow(ru);
        ru.setVisible(true);
    }

    public static void loadEditUserView(String userType) {
        ClientView view = ClientView.getInstance();
        EditUserView ed = new EditUserView(view, userType);
        ed.pack();
        view.centerWindow(ed);
        ed.setVisible(true);
    }

    /**
     * System verifies user input.
     *
     * @param user username to be verified
     * @param pwd password of user
     */
    public boolean verifyInput(String user, String pwd, String access) {
        boolean userExists = false;

        //if user exists, return true
        //else user doesnt exist, return false
        if (connStatus) {
            try {
                QueryModel query = new QueryModel();
                result = query.checkUserLogin(user, pwd, access);
                userExists = result.next();
            } catch (Exception e) {
               ClientView.printMessage("Error: user retrieval fail\n");
               ClientView.printMessage(e.getMessage());
            }
            return userExists;
        } else {
            return userExists;
        }
    }

    /**
     * System removes user from database.
     *
     * @param user username of user to be deleted
     * @param pwd password of user
     */
    public void deleteUser(String user, String pwd, String access) {
        QueryModel query = new QueryModel();
        query.deleteUser(user, pwd, access);
        userUpdate = true;
    }

    public void registerUser(String user, String pwd, String access) {
        QueryModel query = new QueryModel();
        query.createUser(user, pwd, access);
        userUpdate = true;
    }

    public void editUser(String user, String pwd2) {
        QueryModel query = new QueryModel();
        query.editUser(user, pwd2);
    }
    
    public void checkUser(String user) throws SQLException {
        QueryModel query = new QueryModel();
        ResultSet rs = query.registerInput(user);
        userUpdate = rs.next();
    }

    public void updateUser(Boolean status) {
        userUpdate = status;
    }

    /**
     * System notifies actor user has been deleted.
     */
    public boolean notifyUser()
    {
        return userUpdate;
    }
}
